package JoseH;



import java.sql.*;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Shelo
 */
public class Bus {
    
    Conectate con;
  
    public Bus (){
    con = new Conectate();
    }
    
    
    public void nuevoBus(String patente, String rut, String marca, String modelo, String anio, String cap){
        try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("insert into " + 
                    "BUS(PATENTE_BUS, RUT_CHOFER, MARCA_BUS, MODELO_BUS, YEAR_BUS, CAPACIDAD_BUS) " + " values(?,?,?,?,?,?)");            
            pstm.setString(1, patente);
            pstm.setString(2, rut);
            pstm.setString(3, marca);                        
            pstm.setString(4, modelo);  
            pstm.setString(5, anio);
            pstm.setString(6, cap);
            pstm.execute();
            pstm.close();            
            }
        
        catch(SQLException e){
            System.out.println(e);
            }
        }
    
   
    public void deleteBus(String cod){  
        try {                
            PreparedStatement pstm = con.getConnection().prepareStatement("delete from BUS where PATENTE_BUS = ?");            
                pstm.setString(1, cod);                   
                pstm.execute();
                pstm.close();            
            }
        
        catch(SQLException e){
            System.out.println(e);
            }            
        }
    public void updateBus(String patente, String marca, String modelo, String year, String capacidad ){
        try {            
           PreparedStatement pstm = con.getConnection().prepareStatement("update BUS " +
            "set MARCA_BUS = ? , " +                    
            "MODELO_BUS = ? ," + 
            "YEAR_BUS = ? ," +
            "CAPACIDAD_BUS = ? " +
            "where PATENTE_BUS = ? ");  
            pstm.setString(1, marca);
            pstm.setString(2, modelo);
            pstm.setString(3, year);
            pstm.setString(4, capacidad);
            pstm.setString(5, patente);
            pstm.execute();
            pstm.close();            
      }catch(SQLException e){
            System.out.println(e);
      }
    }
    

    
 
public Object [][] getDatos(){
    
        int registros = 0;
     
        try {         
            PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM BUS ");
            ResultSet res = pstm.executeQuery();
            res.next();
            registros = res.getInt("total");
            res.close();
            }
        
        catch(SQLException e){
            System.out.println(e);
        }
      
    Object[][] data = new String[registros][6];  

        try {    
            PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " PATENTE_BUS, RUT_CHOFER, MARCA_BUS, MODELO_BUS, YEAR_BUS, CAPACIDAD_BUS " +
            " FROM BUS" +
            " ORDER BY PATENTE_BUS");
            ResultSet res = pstm.executeQuery();
        
        int i = 0;
            while(res.next()){
            String estPatente = res.getString("PATENTE_BUS");
            String estRut = res.getString("RUT_CHOFER");
            String estMarca = res.getString("MARCA_BUS");
            String estModelo = res.getString("MODELO_BUS");
            String estAnio = res.getString("YEAR_BUS");
            String estCapacidad = res.getString("CAPACIDAD_BUS");
            data[i][0] = estPatente;            
            data[i][1] = estRut;            
            data[i][2] = estMarca;            
            data[i][3] = estModelo;            
            data[i][4] = estAnio;            
            data[i][5] = estCapacidad;
            i++;
            }
            
        res.close();
        }
        
        catch(SQLException e){
            System.out.println(e);
        }
    
        return data;
        
    }
    
}
